package com.ufm.user.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ufm.syslog.pojo.Syslog;
import com.ufm.syslog.service.ISyslogService;
import com.ufm.user.dao.UserMapper;
import com.ufm.user.pojo.User;


@Service
@Transactional
public class UserServicImpl implements IUserService {
	@Autowired  
    private UserMapper userMapper;

	@Autowired
	private ISyslogService syslogService;
	
	@Override
	public List<User> findAll(Map param) {
		return userMapper.findAll(param);
	}
	
	@Override
	public int findAllCount(Map param) {
		return userMapper.findAllCount(param);
	}
	
	@Override
	public User findUserById(long id){
		return userMapper.findUserById(id);
	}

	@Override
	public User insert(User user) {
		userMapper.insert(user);
		return user;
	}

	@Override
	public void delete(long id) {
		userMapper.delete(id);

	}

	@Override
	public void deleteMore(String[] ids, HttpServletRequest request) {
		for (int i = 0; i < ids.length; i++) {
			
			User user = userMapper.findUserById(Long.parseLong(ids[i]));
			//User userSession = (User)request.getSession().getAttribute("user");
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			User userSession = userMapper.findByName(userDetails.getUsername());
			Syslog syslog = new Syslog("人员维护", "删除人员:"+user.getUserName(), 1, userSession.getUserId(), userSession.getUserName(), request.getRemoteAddr(), new Date());
			syslogService.insert(syslog);
			
			userMapper.delete(Long.parseLong(ids[i]));
		}
	}

	@Override
	public User findByName(String name) {
		return userMapper.findByName(name);
	}
	
	@Override
	public void update(User user) {
		userMapper.update(user);
	}
	
	@Override
	public void updatePwd(User user) {
		userMapper.updatePwd(user);
	}
	
	@Override
	public List<String> findAllEmail() {
		List<String> list = new ArrayList<String>();
		list = userMapper.findAllEmail();
		return list;
	}
	
	@Override
	public List<String> findAllUserLogin() {
		List<String> list = new ArrayList<String>();
		list = userMapper.findAllUserLogin();
		return list;
	}

	@Override
	public List<String> hasRight(String userLoginName) {
		return userMapper.hasRight(userLoginName);
	}

	@Override
	public List<User> findByDept(int deptId) {
		return userMapper.findByDept(deptId);
	}
	
	@Override
	public String getGunDongBoBao(){
		return userMapper.getGunDongBoBao();
	}

	@Override
	public int hasOneRight(Map params) {
		return userMapper.hasOneRight(params);
	}

	@Override
	public List<User> getUserIdAndName(Map params) {
		return userMapper.getUserIdAndName(params);
	}
	
	@Override
	public List<User> findAllConAdmin(Map param) {
		return userMapper.findAllConAdmin(param);
	}
	
	@Override
	public int isAdmin(long id){
		return userMapper.isAdmin(id);
	}
	
	@Override
	public int isSelectAllRoles(long id) {
		return userMapper.isSelectAllRoles(id);
	}
	
	@Override
	public void registMore(String[] ids, HttpServletRequest request) {
		for (int i = 0; i < ids.length; i++) {
			
			User user = userMapper.findUserById(Long.parseLong(ids[i]));
			if("0".equals(user.getAttribute1()) || user.getAttribute1() == null){
				user.setAttribute1("1");
				
				UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
				User userSession = userMapper.findByName(userDetails.getUsername());
				Syslog syslog = new Syslog("人员维护", "激活人员:"+user.getUserName(), 1, userSession.getUserId(), userSession.getUserName(), request.getRemoteAddr(), new Date());
				syslogService.insert(syslog);
				
				userMapper.registUser(user);
			}
		}
	}
}
